package com.zzdx.deployment.web.controller.project;

import com.zzdx.deployment.common.annotation.Log;
import com.zzdx.deployment.common.base.AjaxResult;
import com.zzdx.deployment.common.enums.BusinessType;
import com.zzdx.deployment.common.page.TableDataInfo;
import com.zzdx.deployment.common.utils.ExcelUtil;
import com.zzdx.deployment.framework.util.ShiroUtils;
import com.zzdx.deployment.framework.web.base.BaseController;
import com.zzdx.deployment.system.domain.PrjversionConfig;
import com.zzdx.deployment.system.service.IPrjversionConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 项目版本配置 信息操作处理
 * 
 * @author mason
 * @date 2018-12-27
 */
@Controller
@RequestMapping("/project/prjversionConfig")
public class PrjversionConfigController extends BaseController
{
    private String prefix = "project/prjversionConfig";
	
	@Autowired
	private IPrjversionConfigService prjversionConfigService;
	
	@RequiresPermissions("project:prjversionConfig:view")
	@GetMapping()
	public String prjversionConfig()
	{
	    return prefix + "/prjversionConfig";
	}
	
	/**
	 * 查询项目版本配置列表
	 */
	@RequiresPermissions("project:prjversionConfig:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PrjversionConfig prjversionConfig)
	{
		startPage();
        List<PrjversionConfig> list = prjversionConfigService.selectPrjversionConfigList(prjversionConfig);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出项目版本配置列表
	 */
	@RequiresPermissions("project:prjversionConfig:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PrjversionConfig prjversionConfig)
    {
    	List<PrjversionConfig> list = prjversionConfigService.selectPrjversionConfigList(prjversionConfig);
        ExcelUtil<PrjversionConfig> util = new ExcelUtil<PrjversionConfig>(PrjversionConfig.class);
        return util.exportExcel(list, "prjversionConfig");
    }
	
	/**
	 * 新增项目版本配置
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存项目版本配置
	 */
	@RequiresPermissions("project:prjversionConfig:add")
	@Log(title = "项目版本配置", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PrjversionConfig prjversionConfig)
	{		
		return toAjax(prjversionConfigService.insertPrjversionConfig(prjversionConfig));
	}

	/**
	 * 修改项目版本配置
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		PrjversionConfig prjversionConfig = prjversionConfigService.selectPrjversionConfigById(id);
		mmap.put("prjversionConfig", prjversionConfig);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存项目版本配置
	 */
	@RequiresPermissions("project:prjversionConfig:edit")
	@Log(title = "项目版本配置", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PrjversionConfig prjversionConfig) throws Exception {
		String loginName = ShiroUtils.getLoginName();
		prjversionConfig.setUpdateBy(loginName);
		prjversionConfig.setUpdateTime(new Date());
		PrjversionConfig prjversionConfigQuery = new PrjversionConfig();
		prjversionConfigQuery.setProjectVersionId(prjversionConfig.getProjectVersionId());
		List<PrjversionConfig> prjversionConfigs = prjversionConfigService.selectPrjversionConfigList(prjversionConfigQuery);
		int updateRows;
		//若还没有配置，则添加记录
		if(prjversionConfigs.size() == 0){
			prjversionConfigQuery.setCreateBy(loginName);
			prjversionConfigQuery.setCreateTime(new Date());
			updateRows = prjversionConfigService.insertPrjversionConfig(prjversionConfig);
		}else{
			updateRows = prjversionConfigService.updatePrjversionConfig(prjversionConfig);
		}
		return toAjax(updateRows);
	}
	
	/**
	 * 删除项目版本配置
	 */
	@RequiresPermissions("project:prjversionConfig:remove")
	@Log(title = "项目版本配置", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(prjversionConfigService.deletePrjversionConfigByIds(ids));
	}
	
}
